package testshop.page;


import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import sun.applet.Main;
import testshop.dao.GoodsDao;
import testshop.entity.Goods;
import testshop.tools.QueryPrint;
import testshop.tools.ScannerChoice;

import java.util.ArrayList;


public final class GoodsPage extends ScannerChoice {



//   1.添加商品
//
//	 2.更改商品
//
//	 3.删除商品
//
//	 4.查询商品
//
//	 5.显示所有商品

  
      /**
           * 方法实现说明
           * @method      添加商品页面
           * @author      CanZhou
           * @date        2018/3/15 16:45
           */
      
    public  static  void  addGoodsPage(){

        System.out.println("欢迎来到添加商品界面");
        System.out.println("请输入商品的名称");

        String goodsName = ScannerInfoString();

        System.out.println("请输入商品的价格");

        Double goodsPrice = ScannerInfo();


        System.out.println("请输入商品的数量");

        int goodsNum = ScannerNum();

        Goods goods = new Goods(goodsName,goodsPrice,goodsNum);

        GoodsDao dao = new GoodsDao();
        boolean bool =   dao.addGoods(goods);

        if (bool){
            System.out.println("您已成功添加到数据库");
        }else{
            System.out.println("添加失败");
        }

        ScannerChoice.changeInfoNext("addGoodsPage");

    }



    public  static  void  deleteGoodsPage()
    {


        int gid = QueryPrint.query("deleteGoodsPage");

        do {

            System.out.println("确认删除:(Y/N)");

            String choice = ScannerChoice.ScannerInfoString();

            if (choice.toLowerCase().equals("y")){


                boolean bool = new GoodsDao().deleteGoods(gid);

                if (bool){

                    System.out.println("删除商品成功");

                }else{
                    System.out.println("删除商品失败");
                }



                ScannerChoice.changeInfoNext("deleteGoodsPage");


                //删除

            }else if (choice.toLowerCase().equals("n")){


                //回到上一页

                MainPage.goProductMaintenancePage();

            }


            System.out.println("输入错误，请重新输入Y或者N,不区分大小写");



        }while (true);




    }



    public static  void  queryGoosPage(){

        System.out.println("\t\t  正在执行查询商品操作\n");
        System.out.println("\t\t1.按照商品 数量升序 查询");
        System.out.println("\t\t2.按照商品 价格升序 查询");
        System.out.println("\t\t3.输入商品  关键字  查询");
        System.out.println("\n请输入选项,或者按0返回上一级菜单.");


        do {

            String info = ScannerChoice.ScannerInfoString();
            String  regx = "[0-3]";
            if (info.matches(regx)){

                int chioce = Integer.parseInt(info);
                switch (chioce){

                    case 0 :

                        MainPage.goProductMaintenancePage();

                        break;
                    case 1:

                    case 2:

                    case 3:

                        if (chioce == 3 ) {
                            System.out.println("\t\t正在执行商品  关键字  查询操作\n");
                            System.out.println("\n请输入商品关键字");
                        }

                        ArrayList<Goods> list = new GoodsDao().queryGoods(chioce);


                        if ( list == null || list.size() <= 0){

                            System.err.println("\n\t!!您查询的商品不存在!!\n");

                            queryGoosPage();

                        }else{

                            if (chioce == 1) //打印目录，不要放在功能函数中，会影响其他方法调用
                            {
                                System.out.println("\t\t\t\t\t商品按照 数量升序 列表\n\n");
                            }else if (chioce == 2)
                            {
                                System.out.println("\t\t\t\t\t商品按照 价格升序 列表\n\n");
                            }else
                            {
                                System.out.println("\t\t\t\t\t您所查找的商品如下\n\n");
                            }
                            System.out.println("\t商品编号\t\t商品名称\t\t商品价格\t\t商品数量\t\t备注\n");




                            for (Goods goods: list) {



                                System.out.print("\t"+goods.getGid()+"\t\t"+goods.getGname()+"\t\t"+goods.getGprice()+"\t\t"+goods.getGnum());
                                int gnum = goods.getGnum();
                                if (gnum == 0){

                                    System.out.println("\t\t该商品已经售罄");

                                }else if (gnum < 10){
                                    System.out.println("\t\t该商品不足10件");

                                }
                                System.out.println("\t");

                                System.out.println("---------------------------------------------------------------");


                            }

                            do
                            {
                                System.out.println("输入 0 返回上一级菜单");
                                String choiceNext = ScannerInfoString();

                                if ("0".equals(choiceNext))
                                {
                                    MainPage.goProductMaintenancePage();
                                }
                                System.err.println("输入有误！");
                            } while (true);




                        }

                        break;

                    default:break;


                }






            }else {

                System.err.println("输入有误！");
                System.out.println("请重新选择,或者按0返回上一级菜单.");
            }




        }while (true);







    }

    public static void showAllGoodsPage(){

        System.out.println("\t\t所有商品如下\n");

        ArrayList<Goods> list = new GoodsDao().showAllGoods();

        if (list == null || list.size() <= 0 ){

            System.out.println("没有任何商品");

            MainPage.goProductMaintenancePage();


        }else{


            for (Goods goods: list
                 ) {

                System.out.print("\t"+goods.getGid()+"\t\t"+goods.getGname()+"\t\t"+goods.getGprice()+"\t\t"+goods.getGnum());
                int gnum = goods.getGnum();
                if (gnum == 0){

                    System.out.println("\t\t该商品已经售罄");

                }else if (gnum < 10){
                    System.out.println("\t\t该商品不足10件");

                }else if (gnum > 50){

                    System.out.println("\t\t该商品充足");

                }
                System.out.println("\t");

                System.out.println("---------------------------------------------------------------");
            }


            do {

                System.out.println("输入0返回上一级");

                String info = ScannerChoice.ScannerInfoString();

                if (info.matches("0")){

                    MainPage.goProductMaintenancePage();
                }else{

                    System.out.println("输入错误");


                }




            }while (true);


        }











    }

    public  static  void  updateGoodsPage(){

        System.out.println("\t正在执行更改商品操作\n");
        System.out.println("请输入想要更改的商品名字");

        int gid = QueryPrint.query("updateGoodsPage");

        //获得了gid，然后选择修改哪个参数

        System.out.println("\n--------请选择您要更改的内容\n");
        System.out.println("\t1.更改商品-名称");
        System.out.println("\t2.更改商品-价格");
        System.out.println("\t3.更改商品-数量");
        System.out.println("\n请输入选项,或者按0返回上一级菜单.");


        do {

          String c = ScannerChoice.ScannerInfoString();
          String regx = "[0-3]";
          if (c.matches(regx)){

              int choice = Integer.parseInt(c);

              switch (choice){

                  case 0:
                      MainPage.mainPage();
                      break;
                  case 1:

                      System.out.println("请输入商品的新名称");

                      String newProductName = ScannerChoice.ScannerInfoString();
                      Goods goodsName = new Goods(gid,newProductName);
                      boolean boolName = new GoodsDao().updateGoods(choice,goodsName);
                      if (boolName){

                          System.out.println("更新商品名称信息成功");

                      }else{

                          System.out.println("更新商品名称信息失败");

                      }
                      ScannerChoice.changeInfoNext("updateGoodsPage");

                      break;
                  case 2:
                      System.out.println("请输入商品的新价格");

                      double newProductPrice = ScannerChoice.ScannerInfo();
                      Goods goodsPrice = new Goods(gid,newProductPrice);
                      boolean boolPrice = new GoodsDao().updateGoods(choice,goodsPrice);
                      if (boolPrice){

                          System.out.println("更新商品价格信息成功");

                      }else{

                          System.out.println("更新商品价格信息失败");

                      }
                      ScannerChoice.changeInfoNext("updateGoodsPage");
                      break;
                  case 3:
                      System.out.println("请输入商品的数量");

                      int newProductNum = ScannerChoice.ScannerNum();
                      Goods goodsNum = new Goods(gid,newProductNum);
                      boolean boolNum = new GoodsDao().updateGoods(choice,goodsNum);
                      if (boolNum){

                          System.out.println("更新商品数量信息成功");

                      }else{

                          System.out.println("更新商品数量信息失败");

                      }
                      ScannerChoice.changeInfoNext("updateGoodsPage");
                      break;
                  default:
                    break;


              }


          }else{


              System.err.println("！输入有误！");
              System.out.println("请重新选择,或者按0返回上一级菜单.");

          }



        }while (true);











    }






}
